import { defineStore } from 'pinia';
import { pipelineNetworkPieData, pipelineNetworkPieList, staticsList } from '@/api/pipelineNetworkRisk';
import { PieListVo, QueryParams } from '@/api/pipelineNetworkRisk/types';

const riskMap = {
  '极高风险': '1',
  '高风险': '2',
  '中风险': '3',
  '低风险': '4'
};
export const useNetworkRiskGradingStore = defineStore('networkRiskGrading', () => {
  const echartsData = ref([]); // 管网风险等级
  const openRiskPop = ref<boolean>(false); // 打开risks弹窗
  const currentType = ref<string>(''); // risks弹窗当前选中的类型
  const riskList = ref<PieListVo[]>([]); // risks列表
  const currentIndex = ref<number>(-1); // risks列表当前选中的索引
  const pipeDialog = ref<boolean>(false);
  const tableData = ref([]);
  const total = ref<number>(0);
  const loading = ref<boolean>(false);
  const queryParams = ref<QueryParams>({
    pipelineName: '',
    type: 0,
    pageNum: 1,
    pageSize: 10
  });
  //点击echarts的回调
  const pipAgeClick = async (params) => {
    currentIndex.value = -1; // 初始化当前索引
    currentType.value = params.name;
    const { data } = await pipelineNetworkPieList({ type: currentType.value });
    riskList.value = data;
  };
  // 打开risks标题准备弹出对话框
  const pipDialogOpen = () => {
    if (!currentType.value) {
      return ElMessage.warning('请选择要查看的管网风险等级');
    }
    openRiskPop.value = !openRiskPop.value;
  };
  // 获取管网echarts数据
  const getPipelineNetworkPieData = async () => {
    const { data } = await pipelineNetworkPieData();
    echartsData.value = data;
  };
  // 打开risks表格
  const openPipTableHandler = async (item: PieListVo, index: number) => {
    currentIndex.value = index;
    pipeDialog.value = !pipeDialog.value;
    queryParams.value.pipelineName = item.pipelineName;
    queryParams.value.type = riskMap[currentType.value];
    await getList();
  };
  const getList = async () => {
    loading.value = true;
    const { data } = await staticsList(queryParams.value);
    loading.value = false;
    tableData.value = data.records;
    total.value = data.total;
  };
  onMounted(async () => {
    await getPipelineNetworkPieData();
  });
  return {
    echartsData,
    openRiskPop,
    riskList,
    currentIndex,
    queryParams,
    pipeDialog,
    tableData,
    total,
    loading,
    openPipTableHandler,
    getPipelineNetworkPieData,
    pipDialogOpen,
    pipAgeClick,
    getList
  };
});
